package com.lz.ccjdemo.http.model;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import com.google.gson.Gson;
import com.lz.ccjdemo.data.CodeData;
import com.lz.ccjdemo.http.HttpUtils;
import com.lz.ccjdemo.http.data.BaseResultData;
import com.lz.ccjdemo.http.subscriber.SimpleBaseSubscriber;

import java.util.HashMap;
import java.util.List;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 */
public class BaseModel extends AndroidViewModel {

    private CompositeDisposable mCompositeDisposable;

    public BaseModel(@NonNull Application application) {
        super(application);
    }



    public void addDisposable(Disposable disposable) {
        if (this.mCompositeDisposable == null) {
            this.mCompositeDisposable = new CompositeDisposable();
        }
        this.mCompositeDisposable.add(disposable);
    }

    public static RequestBody createRequestBody(HashMap<String, Object> params) {
        String bodyData = new Gson().toJson(params);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), bodyData);
        params.clear();
        return requestBody;
    }

    @Override
    public void onCleared() {
        super.onCleared();
        if (this.mCompositeDisposable != null && !mCompositeDisposable.isDisposed()) {
            this.mCompositeDisposable.clear();
        }
    }

    public void clear(Disposable disposable) {
        if (this.mCompositeDisposable != null && !mCompositeDisposable.isDisposed()) {
            this.mCompositeDisposable.remove(disposable);
        }
    }
}
